<?php
/**
 * FP_Add class file
 * @package FlickrPhotosets
 */

/**
 * Handles the addition of a user to the application.
 * 
 * @package FlickrPhotosets
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */
class FP_Add extends FlickrPhotosets {
	
	/**
	 * Requires that the user has added the application before
	 * attempting to add them to the database.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function __construct() {
		parent::__construct();
		
		// Make sure the user has added the app
		$this->uid = $this->facebook->require_login();
		
		// Are we adding a page?
		/*if ($this->page_id) {
			$this->add_page($this->page_id);
		}
		else {*/
			$this->add_user();
		//}
	}
	
	/**
	 * Add a page, then redirect to config page.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function add_page($page_id) {
		if ($_GET['installed'] == 1) {
			if ($this->set_facebook_info($page_id, $this->facebook->fb_params['session_key'])) {
				$this->log->install($page_id, true, 'page');
				$this->facebook->redirect($this->callback_url.'/configure/?fb_page_id='.$page_id);
			}
		}
	}
	
	/**
	 * Adds the user's UID and session key to the database and
	 * redirects to the appropriate page.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function add_user() {
		
		// For adding on registration
		if ($this->facebook->fb_params['registration'] == 1) {
			if ($this->set_facebook_info($this->uid, $this->facebook->fb_params['session_key'])) {
				$this->log->install($this->uid, true, 'registration');
			}
			else {
				$this->log->install($this->uid, false, 'registration');
			}
		}
		// Regular add, redirect to flickr auth page
		else if ($_GET['installed'] == 1) {
			if ($this->set_facebook_info($this->uid, $this->facebook->fb_params['session_key'])) {
				$this->log->install($this->uid, true, 'regular');
				$this->facebook->redirect($this->callback_url.'/auth.php');
			}
			else {
				/*
				This will most likely cause an infinite loop of redirects 
				because the configuration page requires that the application
				be added. Should only happen if the database is down. Eventually there
				should be some sort of contingency plan. They can't really do
				anything until their info is put in the database, though.
				*/
				$this->facebook->redirect($this->canvas_url.'/');
			}
		}
		// Redirect hackers >:o (that's an angry face)
		else {
			$this->facebook->redirect($this->facebook->get_facebook_url());
		}
		
	}
	
}

?>